package leetcode.nowcoder删除链表中的重复结点;

import leetcode.ListNode;

public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead==null) {
            return null;
        }

        ListNode newHead=new ListNode(-1);
        newHead.next=pHead;

        ListNode prev=newHead;
        ListNode p1=pHead;
        ListNode p2=pHead.next;

        while (p2!=null) {
            if(p1.val!=p2.val) {
                prev=prev.next;
                p1=p1.next;
                p2=p2.next;
            }else {

                while(p2!=null && p1.val==p2.val) {
                    p2=p2.next;
                }
                prev.next=p2;
                p1=p2;
                if (p2!=null) {
                    p2=p2.next;
                }

            }

        }
        return newHead.next;
    }
}
